/*Jt+6kHWNgkqsbBzAWvFn5nQkWOZDKmW3jHO8d+YPvRzoa+JiSAaybGnRiZqBQpBm*/
/**
 * 批改属性选择
 * @author yinshb
 */
import React, { Component } from 'react';
import {base,ajax,toast } from 'nc-lightapp-front';
let { NCTable,NCSelect, NCButton,NCCheckbox,NCCol,NCRow, NCModal, NCCollapse,NCTree,NCFormControl,NCSetColBtn,NCForm  } = base;
const NCOption = NCSelect.NCOption;
const NCFormItem = NCForm.NCFormItem;
import './BatchEditForm.less';

const formid = 'batchupdate';
const specialForm = 'special';

class Batcheditform extends Component {
    constructor(props) {
        super(props);
        this.tableItems = props.tableItems;
        this.state = {
            itemModal : {
                show: false,
                modalDropup: true,
                size: 'xlg',
                fieldid:'batcheditform_modal-area'
            },
            allAttrs : [],
            selectItemCode : [],
            currTabKey:'',
            selectItemName:'',
            isCreateForm : false,
            hasSpecial : false,
            specialItem : {}
        };
        this.props.meta.removeMeta(formid);
    }

    /**
     * 组织页签选择节点内容
     */
    getTabOption = () => {
        let option = [];
        Object.keys(this.tableItems).forEach(item => {
            option.push(<NCOption value={this.tableItems[item].code}>{this.tableItems[item].name}</NCOption>)
        });
        return option;
    }

    /**
     * 页签选择改变事件
     */
    TabChange = (value) => {
        this.setState({
            allAttrs : this.tableItems[value].attrs,
            selectItemCode : [],
            currTabKey : value,
            selectItemName : '',
            specialItem : this.tableItems[value].special,
            hasSpecial : this.tableItems[value].special?true:false
        },()=>{
        //清空表体
        let meta = this.props.meta.getMeta();
        meta[formid] = {
            code : formid,
            moduletype : 'form',
            items : []
        };
        meta[specialForm] = {
            code : specialForm,
            moduletype : 'form',
            items : this.state.hasSpecial?[this.state.specialItem]:[]
        };
        this.props.meta.setMeta(meta,()=>{this.props.form.setFormStatus(specialForm,'edit');});
        });

    }

    /**
     * 打开选择修改属性modal
     */
    openSelectItemModal = () => {
        if(this.state.allAttrs.length === 0){
            this.props.modal.show('modal',{color:'danger',content:'请先选择属性所在页签'});
            return;
        }
        this.state.itemModal.show = true;
        this.setState(this.state);
    }

    /**
     * 选择完成要修改的属性，点击确定事件
     */
    selectItemFinish = () => {
        let n = '';
        this.state.selectItemCode.forEach(code=>{
            this.state.allAttrs.forEach(attr=>{
                if(attr.code === code){
                    if(n === ''){
                        n += attr.name;
                    }else{
                        n += ',' + attr.name;
                    }
                }
            });
        });
        let items = [];
        this.tableItems[this.state.currTabKey].attrItems.forEach(item=>{
            this.state.selectItemCode.forEach(code=>{
                if(code === item.attrcode){
                    items.push(item);
                }
            });
        });
        this.state.itemModal.show = false;
        this.state.selectItemName = n;
        this.state.isCreateForm = true;
        ///////////////////////////////
        let meta = this.props.meta.getMeta();
        meta[formid] = {
            code : formid,
            moduletype : 'form',
            items : items
        }
        this.props.meta.setMeta(meta,()=>{this.setState(this.state)});
        this.props.form.setFormStatus(formid,'edit');
    }

    /**
     * 组织能够修改的属性内容
     */
    getCheckBoxItem = () => {
        let boxes = [];
        this.state.allAttrs.forEach(attr=>{
            boxes.push(<div><NCCheckbox checked={this.state.selectItemCode.indexOf(attr.code)!==-1} onChange={(value)=>{this.changeCheck(value,attr.code)}}>{attr.name}</NCCheckbox></div>);
            });
        return boxes;
    }

    /**
     * 修改属性选择改变事件
     * @param {*} value
     * @param {*} key
     */
    changeCheck (value,key){
        if(value){
            this.state.selectItemCode.push(key);
        }else{
            this.state.selectItemCode.remove(key);
        }
        this.setState(this.state);
    }

    getData = () => {
        if(this.state.allAttrs.length === 0){
            this.props.ncmodal.show('modal',{color:'danger',content:'请先选择属性所在页签'});
            return false;
        }
        let returnData = {};
        returnData.currTabKey = this.state.currTabKey;
        if(this.state.hasSpecial){
            let specialValue = this.props.form.getFormItemsValue(specialForm,this.state.specialItem.attrcode);
            if(!specialValue || !specialValue.value || specialValue.value === ''){
                this.props.ncmodal.show('modal',{color:'danger',content:`请先选择${this.state.specialItem.label}`});
                return false;
            }else{
                returnData.selectedOrgs = specialValue.value.split(',');
            }
        }
        if(this.state.selectItemCode.length === 0){
            this.props.ncmodal.show('modal',{color:'danger',content:'请选择修改属性'});
            return false;
        }
        let isCheck = this.props.form.isCheckNow(formid);
        if(!isCheck){
            return false;
        }else{
            returnData.attrs = this.state.selectItemCode;
            returnData.attrValues = this.props.form.getFormItemsValue(formid,this.state.selectItemCode);
        }
        return returnData;
    }

    render(){

        return (
            <div className="BatchEditForm">
                <div className="inline-block-area">
                    <div className="input-title">批改属性所在页签</div>
                    <div className="input-class">
                        <NCSelect style={{ width: 200, marginRight: 6 }} fieldid="batchupdatetab_select" onChange={this.TabChange} >
                            {this.getTabOption()}
                        </NCSelect>
                    </div>
                </div>
                <div className="org-class">
                    {this.state.hasSpecial && this.props.form.createForm(specialForm)}
                </div>
                <div className="inline-block-area">
                    <div className="input-title">批改属性</div>
                    <div className="input-class">
                        <NCFormControl type="text" disabled={true} value={this.state.selectItemName}/>
                    </div>
                    <div className="button-class"><NCSetColBtn onClick={ this.openSelectItemModal }></NCSetColBtn></div>
                </div>
                <div>
                    {this.state.isCreateForm && this.props.form.createForm(formid)}
                </div>

                <NCModal {...this.state.itemModal} fieldid = 'batcheditform'>
                    <NCModal.Header closeButton={false}>
                        <NCModal.Title>批改属性</NCModal.Title>
                    </NCModal.Header>
                    <NCModal.Body>
                        <div style={{maxHeight:'400px'}}>{this.getCheckBoxItem()}</div>
                    </NCModal.Body>
                    <NCModal.Footer>
                        <span><NCButton onClick={ this.selectItemFinish}>确定</NCButton></span>
                        <NCButton onClick={ ()=>{this.state.itemModal.show=false;this.setState(this.state);} }>取消</NCButton>
                    </NCModal.Footer>
                </NCModal>

                {this.props.ncmodal.createModal('modal', {
                    color:'danger', // 值： "success"/"info"/"warning"/"danger"
                    title:"提示",
                    content:'',
                    beSureBtnClick: ()=>{this.props.modal.close('modal')},
                    cancelBtnClick: ()=>{this.props.modal.close('modal')}
                })}
            </div>
        )
    }

}

export default Batcheditform;


/*Jt+6kHWNgkqsbBzAWvFn5nQkWOZDKmW3jHO8d+YPvRzoa+JiSAaybGnRiZqBQpBm*/
